package com.lzs.collection.Set;

import lombok.Data;

import java.util.HashSet;
import java.util.Objects;

@SuppressWarnings("all")
public class HashSetExercise {
    public static void main(String[] args) {
        HashSet<Object> set = new HashSet<>();
        set.add(new Employee("milam",18));
        set.add(new Employee("smith",28));
        set.add(new Employee("milam",18));
        //set集合中加入了几个元素? 2个，最后一个milan加入不成功！
        System.out.println(set);


    }
}
@Data
class Employee {
    private String name;
    private int age;
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //若name和age值相同，则返回相同的hash值
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age==employee.age&& Objects.equals(name,employee.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name,age);
    }
}
